# Maintainer: Daurnimator <daurnimator@archlinux.org>

pkgname=argocd
pkgver=2.9.1
# find matching argo-ui commit via ui/yarn.lock
# commit id also needs to be updated in argo-ui.patch
_argoui_commit=002d01b18e8aaf4b21307a3b87341ab05230483f
pkgrel=1
pkgdesc="Declarative continuous deployment for Kubernetes"
arch=('loong64' 'x86_64')
url='https://argo-cd.readthedocs.io/en/stable/'
license=('Apache')
makedepends=('go' 'python' 'yarn')
depends=('glibc')
provides=('argocd-cli')
replaces=('argocd-cli')
source=("argo-cd-${pkgver}.tar.gz::https://github.com/argoproj/argo-cd/archive/v${pkgver}.tar.gz"
        "argo-ui-${_argoui_commit}.tar.gz::https://github.com/argoproj/argo-ui/archive/${_argoui_commit}.tar.gz"
        "argo-ui.patch")
sha512sums=('9d3c45e9c19fea56312abbc2d57b5c34ffe9116fbc28622e496c8000e13cb0ed4c210f7ee2a7650d13cd905d8923b0ab19c1c9511946b0200fce307c80624851'
            '683dc7b159fc0ae92be1623c5a48c43c38f42659c53703ff43d62e42192e875fbed3494872235cb1cd8fe096399aedd8f80c18d36a3901cd08dbd6c7d60b5a14'
            '82c737b8d4fa134aee5ea53df79ae4ec4b872e597f2adef919dae2afe07fcfd0ca9659f9334864f19e7618fd8b9b225b1c82dbaccf9a2fb1e18dbb1dd830fc2e')

prepare() {
  cd "argo-cd-$pkgver"

  patch -p0 -i ../argo-ui.patch

  export GOPATH="${srcdir}"
  go mod download
}

build() {
  cd "argo-cd-$pkgver"

  (
    cd ui
    yarn install --cache-folder "${srcdir}/yarn-cache"
    ARGO_VERSION="$pkgver" NODE_ENV='production' NODE_ONLINE_ENV='online' yarn build
  )

  export CGO_CPPFLAGS="${CPPFLAGS}"
  export CGO_CFLAGS="${CFLAGS}"
  export CGO_CXXFLAGS="${CXXFLAGS}"
  export CGO_LDFLAGS="${LDFLAGS}"
  export GOPATH="${srcdir}"
  export GOPROXY=off
  go build -o argocd \
    -trimpath \
    -buildmode=pie \
    -mod=readonly \
    -modcacherw \
    -ldflags "-X github.com/argoproj/argo-cd/v2/common.version=${pkgver} \
              -X github.com/argoproj/argo-cd/v2/common.buildDate=$(date -u +'%Y-%m-%dT%H:%M:%SZ' --date=@${SOURCE_DATE_EPOCH}) \
              -X github.com/argoproj/argo-cd/v2/common.gitTag=${pkgver} \
              -compressdwarf=false \
              -linkmode=external \
              -extldflags \"${LDFLAGS}\"" \
    ./cmd

  ./argocd completion bash > argocd.bash
  ./argocd completion zsh > argocd.zsh
}

package() {
  cd "argo-cd-$pkgver"

  install -D argocd "$pkgdir/usr/bin/argocd"
  ln -s argocd "$pkgdir/usr/bin/argocd-server"
  ln -s argocd "$pkgdir/usr/bin/argocd-application-controller"
  ln -s argocd "$pkgdir/usr/bin/argocd-repo-server"
  ln -s argocd "$pkgdir/usr/bin/argocd-cmp-server"
  ln -s argocd "$pkgdir/usr/bin/argocd-dex"
  ln -s argocd "$pkgdir/usr/bin/argocd-notifications"
  ln -s argocd "$pkgdir/usr/bin/argocd-git-ask-pass"

  install -Dm644 argocd.bash "$pkgdir/usr/share/bash-completion/completions/argocd"
  install -Dm644 argocd.zsh "$pkgdir/usr/share/zsh/site-functions/_argocd"
}
